local cuike = fk.CreateSkill {

  name = "joy__cuike",

  tags = {  },

}



cuike:addEffect(fk.EventPhaseStart, {
  name = "joy__cuike",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(cuike.name) and player.phase == Player.Play
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getMark("@junlue") % 2 == 1 then
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper),
      1, 1, "#cuike-damage", cuike.name, true)
      if #tos > 0 then
        room:damage {
          from = player, to = room:getPlayerById(tos[1]),
          damage = 1, skillName = cuike.name
        }
      end
    else
      local targets = {}
      for _, p in ipairs(room.alive_players) do
        if not (p:isAllNude() and p.chained) then
          table.insert(targets, p.id)
        end
      end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#cuike-discard", cuike.name, true)
      if #tos > 0 then
        local to = room:getPlayerById(tos[1])
        if not to:isAllNude() then
          local cid = room:askForCardChosen(player, to, "hej", cuike.name)
          room:throwCard(cid, cuike.name, to, player)
        end
        if not (to.dead or to.chained) then
          to:setChainState(true)
        end
      end
    end
    if player.dead then return false end
    if player:getMark("@junlue") > #room.alive_players then
      if room:askForSkillInvoke(player, cuike.name, nil, "#cuike-shenfen") then
        room:setPlayerMark(player, "@junlue", 0)
        for _, p in ipairs(room:getOtherPlayers(player)) do
          if not p.dead then
            room:damage {
              from = player, to = p,
              damage = 1, skillName = cuike.name
            }
          end
        end
      end
    end
  end,
})

return cuike